<?php
require_once '../include/common.inc.php';
require_once '../include/upfile.class.php';

if (!checkAdmin()) {
	header("LOCATION:index.php");
	exit;
}

$menu_index = 1;
$page_title = 'JANSEN';
$guide_message = 'Product | Setting';
$out_ary = array();
$sql_where = '';

getMyParam('action');

if ($action != "") 
{
	if ($action == 'add') 
	{
		if(!empty($_POST["name"]))
		{
			/*$file_name = '';
			if($_FILES['photo']['size'] > 0 && trim($_FILES['photo']['name']) != ""){
				$upfile = new uploadFile($_FILES['photo']);
				$upfile->setFileName('J_Setting_'.$timestamp);
				$upfile->setFileType(array('image/pjpeg', 'image/gif', 'image/jpeg'));
				$upfile->setAcceptOverwrite(0);
				//$upfile->setStoreDir(JANSEN_ROOT.'attachments/');
				$upfile->setMaxSize(1024*1024*2);
				
				if (!$upfile->save()) {
					echo "<script>alert('" . $upfile->error_msg . "');history.back(-1);</script>";
					exit;
				}
				$file_name =$upfile->fileName;
			}*/
			
			//$sql = "INSERT INTO `p_setting`(`name`,`photo`,`ispublish`,`createtime`) VALUES('".$_POST["name"]."','".$file_name."','".$_POST["ispublish"]."','".date("yyyy-mm-dd H:i:s")."')";	
			$sql = "INSERT INTO `p_setting`(`name`,`namezh`,`ispublish`,`createtime`) VALUES('".$_POST["name"]."','".$_POST["namezh"]."','".$_POST["ispublish"]."','".date("yyyy-mm-dd H:i:s")."')";	
			$db->query($sql);
		}
		header("LOCATION:" . $PHP_SELF);
	}
	elseif($action == 'edit') 
	{
		getMyParam('id');
		
		/*$file_name = '';
		if($_FILES['photo']['size'] > 0 && trim($_FILES['photo']['name']) != ""){
			$upfile = new uploadFile($_FILES['photo']);
			$upfile->setFileName('J_Setting_'.$timestamp);
			$upfile->setFileType(array('image/pjpeg', 'image/gif', 'image/jpeg'));
			$upfile->setAcceptOverwrite(0);
			//$upfile->setStoreDir(JANSEN_ROOT.'attachments/');
			$upfile->setMaxSize(1024*1024*2);
			
			if (!$upfile->save()) {
				echo "<script>alert('" . $upfile->error_msg . "');history.back(-1);</script>";
				exit;
			}
			$file_name =$upfile->fileName;
			
			if(file_exists(B_PIC_DIR.$_POST["oldPhoto"]))
				unlink(B_PIC_DIR.$_POST["oldPhoto"]);
		}
		if($file_name != '')
			$file_name = " `photo`='".$file_name."',";
		
		$sql = "UPDATE `p_setting` SET `name`='".$_POST["name"]."',".$file_name."`ispublish`=".$_POST["ispublish"]." WHERE `id`=$id";	*/
		
		$sql = "UPDATE `p_setting` SET `name`='".$_POST["name"]."', `namezh`='".$_POST["namezh"]."',`ispublish`=".$_POST["ispublish"]." WHERE `id`=$id";	
		$db->query($sql);	
		
		goFromPage();
	}
	elseif($action == 'del') {
		getMyParam('id');
		if (is_numeric($id) && $id > 0) {
			$sqlGet = "SELECT photo FROM `p_setting` WHERE `id` = " . $id;
			$rs = $db->get_one($sqlGet);
			/*if(file_exists(B_PIC_DIR.$rs["photo"]))
				unlink(B_PIC_DIR.$rs["photo"]);*/
			
			$sql = "DELETE FROM `p_setting` WHERE `id` =". $id;
			$db->query($sql);
		}
		
		goFromPage();
	}
	
}

// *** page code start ***

if ($action == "search" && trim($Search)  != "" && trim($Search)  != "Enter Name ...") {
	$sql_where = " `name` LIKE '%$Search%'";
}
if($sql_where !='') $sql_where = " WHERE ".$sql_where;

$page_url = 'p_setting.php?';

$sql = "SELECT `id` FROM `p_setting`".$sql_where;
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

$ppp = 100;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
	$start_limit = 0;
	$page = 1;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

// *** page code end ***

//$sql = "SELECT `id`, `name`,`photo`,`ispublish`, `createtime` FROM `p_setting`".$sql_where." ORDER BY `createtime` ASC LIMIT $start_limit, $ppp";
$sql = "SELECT `id`, `name`,`namezh`,`ispublish`, `createtime` FROM `p_setting`".$sql_where." ORDER BY `createtime` ASC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$out_ary[] = $rs;
}
$debug = 1;
debuginfo();

include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_p_setting');
require_once PrintEot('a_footer');
?>